package com.ruoyi.mp.controller;


import com.ruoyi.common.core.constant.MsgConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.EncryptAesUtil;
import com.ruoyi.common.core.utils.QRCodeUtils;
import com.ruoyi.common.core.utils.ShareCodeUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.db.domain.system.MpClient;
import com.ruoyi.db.domain.web.dto.MpRegistForm;
import com.ruoyi.mp.service.IMpClientLoginService;
import com.ruoyi.mp.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(value = "【登录注册】",tags ="【登录注册】")
@RestController
@RequestMapping("/mp/login")
@CrossOrigin
public class MpLoginController extends BaseController {

    private static Logger log = LoggerFactory.getLogger(MpLoginController.class);


    @Autowired
    private IMpClientLoginService mpClientLoginService;
    @Autowired
    private JwtUtils jwtUtils;
    /**
     * 发送短信
     */
    @ApiOperation(value = "用户注册发送短信")
    @GetMapping ("/sendSms")
    public R registSendSms (String mobile,Integer type)
    {

        Boolean isSend = mpClientLoginService.sendSmsByMobile(mobile,type);
          if(isSend){
              return R.ok(null,MsgConstants.SMS_SUCCESS_SEND);
          }
          return R.fail("短信发送失败");
    }
    /**
     * 注册
     */
    @ApiOperation(value = "注册")
    @PostMapping("/register")
    public AjaxResult register(@RequestBody MpRegistForm form)
    {
        mpClientLoginService.clientRegister(form);
        return AjaxResult.success("注册成功");
    }


    /**
     * 登陆
     */
    @ApiOperation(value = "登陆")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody MpRegistForm form)
    {
        MpClient client = mpClientLoginService.clientLogin(form);
        if(client.getStatus().equals("0")){
            return  AjaxResult.error("账号被停用");
        }
        String token = jwtUtils.generateToken(client.getClientId());
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("expire", jwtUtils.getExpire());
        map.put("info",client);
        return  AjaxResult.success(map);
    }
    /**
     * 重置
     */
    @ApiOperation(value = "重置密码")
    @PostMapping("/password/reset")
    public AjaxResult resetPwd(@RequestBody MpRegistForm form)
    {
        mpClientLoginService.passwordReset(form);
        return AjaxResult.success("重置密码成功");
    }

}
